<?php

/**
 * uniBoard Login manager
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class class_login extends class_api {
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * User login
	 *
	 * @var string
	 */
	
	public $user_login 		= NULL;
		
	/**
	 * User password
	 *
	 * @var string
	 */
	
	private $user_pass 		= NULL;
		
	/**
	 * Remember user?
	 *
	 * @var bool
	 */
	
	private $user_remember 	= false;
	
	/**
	 * Log in as hidden?
	 *
	 * @var bool
	 */
	
	private $user_hidden 	= false;
	
	/**
	 * User ACL?
	 *
	 * @var array
	 */
	
	public $user_acl 		= array();
	
	/**
	 * creates error handler
	 *
	 */
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set user login
	 *
	 * @param string $user_login
	 */
	
	function setUserLogin( $user_login){
		
		// Is login empty?
		if ( !isset( $user_login[0]))
		{
			// No login
			$this -> setError( $this -> appCore -> lang['login_form_message_empty_login']);
		}
		else
		{
			// Store in object
			$this -> user_login = $user_login;	
		}
			
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set user pass
	 *
	 * @param string $user_login
	 */
	
	function setUserPass( $user_pass){
		
		// Is password empty?
		if ( !isset( $user_pass[0]))
		{
			// No password
			$this -> setError( $this -> appCore -> lang['login_form_message_empty_login']);
		}
		else
		{
			// Store in object
			$this -> user_pass = $user_pass;	
		}
			
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set log-in options
	 *
	 * @param bool $user_hidden
	 * @param bool $user_remember
	 */
	
	function setUserOps( $user_hidden, $user_remember){
		
		// Configuration allows hidden log-in?
		if ( $this -> appCore -> conf['session_allow_hide'])
		{
			$this -> user_hidden = (bool) $user_hidden;
		}
		
		// Configuration allows automatic log-in?
		if ( $this -> appCore -> conf['board_autolog_allow'])
		{
			$this -> user_remember = (bool) $user_remember;
		}
			
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Authenticates user
	 *
	 */
	
	function authUser(){
		
		// Any errors so far?
		if ( $this -> error == false)
		{
			// Select user from DB
			$this -> appCore -> db -> query( 'SELECT user_id, user_login, user_password, user_salt, user_email, user_activation, user_groups, user_masks, user_acl_cache
			FROM ' . DB_PREFIX . 'users
			WHERE `user_login` LIKE \'' . $this -> appCore -> db -> strEscape( $this -> user_login, true) . '\' AND `user_id` > \'0\'');
				
			// Parse result
			if ( $user_result = $this -> appCore -> db -> fetch_assoc())
			{
				// Free memory
				$this -> appCore -> db -> freeResult();
				
				// User has Permissions?
				if ( isset( $user_result['user_acl_cache'][0]))
				{
					// Load Cache
					$user_acl = (array) unserialize( $user_result['user_acl_cache']);
				}
				else
				{
					// Start ACL builder
					$acl_builder = new class_acl_builder( $this -> appCore);
					
					// Make Perms
					$user_acl = $acl_builder -> makeACL( explode( ',', $user_result['user_groups']), explode( ',', $user_result['user_masks']));
					
					// Update User's ACL
					$this -> appCore -> db -> update( array( 'user_acl_cache' => $this -> appCore -> db -> strEscape( serialize( $user_acl))), 'users', 'user_id = \'' . $user_result['user_id'] . '\'');
					
					// Kill builder
					unset( $acl_builder);
				}
				
				// Check error
				if ( $user_result['user_password'] != $this -> appCore -> hashPassword( $this -> user_pass, $user_result['user_salt']))
				{
					// Incorrect password
					$this -> setError( $this -> appCore -> lang['login_form_message_user_not_found']);
					
					// Register Failed Log In Attempt
					if ( $this -> appCore -> conf['sessions_block_attempts'] > 0)
					{
						$this -> appCore -> db -> insert( array( 'attempt_ip' => $this -> appCore -> db -> strEscape( $this -> appCore -> client_ip), 'attempt_date' => $this -> appCore -> time), 'login_attempts');
					}
				}
				else if ( !IN_ADMIN && $this -> appCore -> checkBan( $user_result['user_id'], true))
				{
					// User PWT
					$this -> setError( isset( $this -> appCore -> ban_reason[0]) ? $this -> appCore -> ban_reason : $this -> appCore -> lang['login_form_message_banned_id']);
				}
				else if ( !IN_ADMIN && $this -> appCore -> checkBan( $user_result['user_email'], false, true))
				{
					// User PWT
					$this -> setError( isset( $this -> appCore -> ban_reason[0]) ? $this -> appCore -> ban_reason : $this -> appCore -> lang['login_form_message_banned_email']);
				}
				else if ( $user_result['user_activation'] > 0)
				{
					// User Not Active
					$this -> setError( $this -> appCore -> lang['login_form_message_user_not_active']);
				}
				else if ( IN_ADMIN && !$user_acl['is_admin'])
				{
					// User Not Admin
					$this -> setError( $this -> appCore -> lang['login_form_message_session_not_admin']);
				}
				else
				{	
					// If we are here, evertyhing is all right, lets make new session
					if ( IN_ADMIN)
					{
						// New admin session
						$this -> appCore -> session -> newAdminSession( $user_result['user_id']);	
					}
					else
					{
						// Promote our session
						$this -> appCore -> session -> updateSession( $user_result['user_id'], $this -> user_hidden);
						
						// Set Autologin key?
						if ( $this -> user_remember && $this -> appCore -> conf['board_autolog_allow'])
						{
							// Generate key
							$session_key = $this -> appCore -> makeSalt( 24, false);
							
							// Insert those into DB
							$this -> appCore -> db -> insert( array(
								'key_id'			=> $session_key,
								'user_id'			=> $user_result['user_id'],
								'key_last_use'		=> $this -> appCore -> time,
								'key_hidden'		=> $this -> user_hidden
							), 'sessions_keys');
							
							// Set Cookies
							$this -> appCore -> setCookie( 'session_key', $user_result['user_id'] . ':' . $session_key, true);
						}
					}
				}
			}
			else
			{
				// User not found
				$this -> setError( $this -> appCore -> lang['login_form_message_user_not_found']);
			}
			
			// Set Log?
			if ( IN_ADMIN)
			{
				// Set Log
				$this -> appCore -> logAdminAccess( ( $this -> error ? -1 : $user_result['user_id']), $this -> user_login, $this -> error == false);
			}
		}
			
	}
	
	/**
	 * logs out user
	 *
	 */
	
	function logout(){
		
		//destroy session
		$this -> unisolutions -> session -> destroySession();
				
	}
	
}